package com.weiyi.mashangzhu.Roomer.Activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Adapter.CollectAdapter;
import com.weiyi.mashangzhu.Roomer.Adapter.OrderAdapter;
import com.weiyi.mashangzhu.Roomer.Adapter.SysMsgAdapter;
import com.weiyi.mashangzhu.Roomer.Bean.CollectInfoBean;
import com.weiyi.mashangzhu.Roomer.Bean.OrderBean;
import com.weiyi.mashangzhu.Roomer.Bean.SysMessageBean;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SysMsgActivity extends BaseActivity {


    @BindView(R.id.refresh_list)
    PullToRefreshListView refreshList;
    private int page = 1;
    List<SysMessageBean.DataBean> list = new ArrayList<>();
    SysMsgAdapter sysMsgAdapter = null;
    private String loginLeve;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sys_msg);
        loginLeve = (String) SPUtils.get(SysMsgActivity.this, "loginLeve", "1");
        ButterKnife.bind(this);
        initView();
    }

    @Override
    public void initView() {
        super.initView();
        initData(1);

        refreshList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                Log.e("TAG", "onPullDownToRefresh");
                //这里写下拉刷新的任务
                new DownGetDataTask().execute();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                Log.e("TAG", "onPullUpToRefresh");
                new UpGetDataTask().execute();
            }
        });
    }

    private void initData(final int i) {
        OkHttpUtils.get().addParams("token", SPUtils.get(this, "token", "") + "")
                .addParams("uCid", SPUtils.get(this, "cid", "") + "")
                .addParams("pagenum", i + "")
                .url(API.BaseApi_R + API.SysMessage_Api)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        T.showShort(SysMsgActivity.this, "网络连接失败，请检查一下网络设置");
                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            if (jsonObject.getInt("status") == 1) {
                                T.showShort(SysMsgActivity.this, jsonObject.getString("msg"));
//                                SPUtils.put(CouponActivity.this, "isLogin", "0");
                            } else {
                                Gson gson = new Gson();
                                SysMessageBean sysmessage = gson.fromJson(response, SysMessageBean.class);
                                List<SysMessageBean.DataBean> data = sysmessage.getData();

                                if (data != null && data.size() > 0) {
                                    for (int j = 0; j < data.size(); j++) {
                                        SysMessageBean.DataBean dataBean = data.get(j);
                                        if (dataBean != null) {
                                            if (loginLeve != null) {
                                                if (!loginLeve.equals(dataBean.getMsgLeve())) {
                                                    if (!"1".equals(dataBean.getMsgLeve())) {
                                                        data.remove(j--);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }

                                if (i == 1) {
                                    list.clear();
                                    list.addAll(data);
                                    sysMsgAdapter = new SysMsgAdapter(list, SysMsgActivity.this);
                                    refreshList.setAdapter(sysMsgAdapter);
                                } else {
                                    list.addAll(data);
                                    sysMsgAdapter.notifyDataSetChanged();
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private class DownGetDataTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            try {
                Thread.sleep(1000);


            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            page = 1;
            initData(page);

            refreshList.onRefreshComplete();
        }
    }

    private class UpGetDataTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            try {
                Thread.sleep(1000);

            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            page += 1;
            initData(page);
            refreshList.onRefreshComplete();
        }
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}